//
//  BaiChuanShare.m
//  danpin
//
//  Created by chuangjia on 21/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "BaiChuanShare.h"

@implementation BaiChuanShare
+ (instancetype)sharedInstance
{
    static BaiChuanShare *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[BaiChuanShare alloc] init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
       
    }
    return self;
}
-(void)dealloc
{
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)initSDK:(BOOL)isDebug
{
    if (isDebug) {
        [[TaeSDK sharedInstance] setDebugLogOpen:isDebug];
    }
    //sdk初始化
    [[TaeSDK sharedInstance] asyncInit:^{
        DLog(@"初始化成功");
    } failedCallback:^(NSError *error) {
        DLog(@"初始化失败:%@",error);
    }];
}
-(BOOL)handleOpenURL:(NSURL *)url
{
    
    return [[TaeSDK sharedInstance] handleOpenURL:url];
}
//插件版登录状态监听
-(void)setSessionStateChangedHandler
{
    id<ALBBLoginService> loginService = [[TaeSDK sharedInstance]getService:@protocol(ALBBLoginService)];
    [loginService setSessionStateChangedHandler:^(TaeSession *session) {
        if([session isLogin]){//未登录变为已登录
            DLog(@"【插件版监听：用户login】");
        }else{//已登录变为未登录
            DLog(@"【插件版监听：用户logout】");
        }
    }];
}
-(void)loginTaoBao:(id)sender success:(void(^)(id result))success failure:(void(^)(NSError* error))failure
{
    if(![[TaeSession sharedInstance] isLogin]){
        id<ALBBLoginService> loginService = [[TaeSDK sharedInstance]getService:@protocol(ALBBLoginService)];
        [loginService showLogin:sender successCallback:^(TaeSession *session) {
            NSString *tip=[NSString stringWithFormat:@"登录的用户信息:%@,登录时间:%@",[session getUser],[session getLoginTime]];
            DLog(@"%@", tip);
            NSMutableDictionary *dict =[NSMutableDictionary dictionary];
            [dict setValue:[NSString stringWithFormat:@"%@",[session getUser].nick] forKey:@"nick_name"];
            [dict setValue:[NSString stringWithFormat:@"%@",[session getUser].iconUrl] forKey:@"user_face"];
            [dict setValue:[NSString stringWithFormat:@"%@",[session getUser].userId] forKey:@"third_id"];
            success(dict);
        } failedCallback:^(NSError *error) {
            DLog(@"%@", error);
            failure(error);
        }];
    }
}
-(void)logout
{
    id<ALBBLoginService> loginService = [[TaeSDK sharedInstance]getService:@protocol(ALBBLoginService)];
    [loginService logout];
}
/*商品详情页，
 *itemId 使用商品id
 *itemType 商品类型:1代表淘宝，2代表天猫
 */
-(void)showTaoKeItemDetail:(id)sender ItemId:(NSString*)itemId itemType:(NSString*)itemType
{
    
    TaeTaokeParams *taoKeParams=[[TaeTaokeParams alloc] init];
    taoKeParams.pid =PID;
    id<ALBBItemService> itemService=[[TaeSDK sharedInstance]getService:@protocol(ALBBItemService)];
    [itemService showTaoKeItemDetailByItemId:sender
                                  isNeedPush:YES
                           webViewUISettings:nil
                                      itemId:[[[NSNumberFormatter alloc]init] numberFromString:itemId]
                                    itemType:[itemType integerValue]
                                      params:nil
                                 taoKeParams:taoKeParams
                 tradeProcessSuccessCallback:^(TaeTradeProcessResult *tradeProcessResult) {
                     DLog(@"交易成功");
                 } tradeProcessFailedCallback:^(NSError *error) {
                     DLog(@"交易失败，错误码是：%ld，错误原因是：%@",(long)error.code,error.description);
                 }];
    
   
}
/**
 *  使用TaeSDK的webview打开H5页面，可以自动实现淘宝安全免登
 *
 *  @param parentController  app当前的Controller
 *  @param isNeedPush            是否需要使用parentController进行push
 *  @param pageUrl                  页面的url
 *  @param webViewUISettings        可以自定义的webview配置项
 */
-(void) showPage:(UIViewController *) parentController isNeedPush:(BOOL) isNeedPush openUrl:(NSString *)openUrl
{
    TaeTaokeParams *taoKeParams=[[TaeTaokeParams alloc] init];
    taoKeParams.pid =PID;
    id<ALBBItemService> itemService=[[TaeSDK sharedInstance]getService:@protocol(ALBBItemService)];
    [itemService showPage:parentController isNeedPush:isNeedPush openUrl:openUrl];
}

/**
 *  使用TaeSDK的webview打开H5页面，可以自动实现淘宝安全免登
 *
 *  @param parentController  app当前的Controller
 *  @param isNeedPush            是否需要使用parentController进行push
 *  @param pageUrl                  页面的url
 *  @param webViewUISettings        可以自定义的webview配置项
 *  @param tradeProcessSuccessCallback    交易流程成功完成订单支付的回调
 *  @param tradeProcessFailedCallback  交易流程未完成的回调
 */
-(void) showPage:(UIViewController *) parentController
      isNeedPush:(BOOL) isNeedPush
         pageUrl:(NSString *)pageUrl
webViewUISettings:(TaeWebViewUISettings *)webViewUISettings
tradeProcessSuccessCallback:(tradeProcessSuccessCallback)tradeProcessSuccessCallback
tradeProcessFailedCallback:(tradeProcessFailedCallback)tradeProcessFailedCallback
{
    TaeTaokeParams *taoKeParams=[[TaeTaokeParams alloc] init];
    taoKeParams.pid =PID;
    id<ALBBItemService> itemService=[[TaeSDK sharedInstance]getService:@protocol(ALBBItemService)];
    [itemService showPage:parentController isNeedPush:isNeedPush pageUrl:pageUrl webViewUISettings:webViewUISettings tradeProcessSuccessCallback:tradeProcessSuccessCallback tradeProcessFailedCallback:tradeProcessFailedCallback];
}

@end
